package com.snj.twelve;

public class SumCalculator {

    /**
     * 计算从 n 到 1 的累加和
     *
     * @param n 传入的整数
     * @return 累加和
     * @throws IllegalArgumentException 如果传入的整数小于 1
     */
    public static int calculateSum(int n) {
        if (n < 1) {
            throw new IllegalArgumentException("传入的整数必须大于或等于 1");
        }

        int sum = 0;
        for (int i = n; i >= 1; i--) {
            sum += i;
        }
        return sum + n;
    }

    public static void main(String[] args) {
        int number = 22;
        int result = calculateSum(number);
        System.out.println(number + " + " + (number - 1) + " + ... + 1 = " + result);
    }
}